package cn.ruituo.jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class QueryStudent {
    //预编译的上下文
    //占位符机制 排序读取
    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.jdbc.Driver");//Class
        } catch (ClassNotFoundException e) {
            System.out.println("没有找到驱动");
            e.printStackTrace();
            //System.exit(0);
            return;
        }
        String url="jdbc:mysql://localhost:3306/java72db?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2b8&rewriteBatchedStatements=true";
        Connection con=null;
        PreparedStatement st=null;
        ResultSet rs=null;

        String updateSql="select * from s_student order by ?";
        System.out.println(updateSql);
        try {
            con= DriverManager.getConnection(url,"root","root1234");
            System.out.println(con.getClass().getName());
            st=con.prepareStatement(updateSql);
            //给占位符填值
            //不起作用
            st.setString(1,"sname");
            //st.setDate();

            rs=st.executeQuery();
            while(rs.next()){
                System.out.println(rs.getLong("stuno")+","+rs.getString("sname")+","+rs.getDate("birthday"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                st.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
